package com.learn.xueer.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.learn.xueer.R
import com.learn.xueer.app.network.apiService
import com.learn.xueer.data.model.bean.BookSectionResponse
import com.learn.xueer.data.model.bean.HearSayQuestionResponse
import com.learn.xueer.data.model.bean.HearSayResponse
import com.learn.xueer.data.model.bean.HomeInfoResponse
import com.learn.xueer.data.model.bean.SectionDetailResponse
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.IntObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.MediaType
import okhttp3.RequestBody

class RequestHearViewModel : BaseViewModel() {
    var hearSayResult = MutableLiveData<ResultState<MutableList<HearSayResponse>?>>()
    var bookSectionResult = MutableLiveData<ResultState<MutableList<BookSectionResponse>?>>()
    var sectionDetailResult = MutableLiveData<ResultState<SectionDetailResponse?>>()
    var hearsayQuestionResult = MutableLiveData<ResultState<HearSayQuestionResponse?>>()
    val bookInfoName = StringObservableField()
    val bookInfoDesc = StringObservableField()
    val bookInfoCurrentSection = StringObservableField("0")
    val bookInfoTotalSection = StringObservableField()
    val drawableUrl = IntObservableField(R.mipmap.icon_asc)
    val sectionTitle = StringObservableField()
    val sectionDetailTitle = StringObservableField()

    val testTitle = StringObservableField()
    val testTemplate = StringObservableField()
    val questionTrunk = StringObservableField()
    val questionTrunkUrl = StringObservableField()
    val secondDuration = StringObservableField()
    val secondDurationStr = StringObservableField()
    val testIcon = IntObservableField()

    fun hearSayReq(params: HashMap<String, Any>) {
        request(
            {
                val requestBody = RequestBody.create(
                    MediaType.get("multipart/form-data"),
                    Gson().toJson(params)
                )
                apiService.getHearSay(requestBody)
            },
            hearSayResult,
            true,
            "正在加载..."
        )
    }

    fun bookSectionReq(id: String, order: String) {
        request(
            {

                apiService.bookSection(id, order)
            },
            bookSectionResult,
            true,
            "正在加载..."
        )
    }

    fun sectionDetailReq(id: String, pid: String) {
        request(
            {
                apiService.sectionDetail(id, pid)
            },
            sectionDetailResult,
            true,
            "正在加载..."
        )
    }

    fun hearsayQuestionReq(id: String) {
        request(
            {
                apiService.hearsayQuestions(id)
            },
            hearsayQuestionResult,
            true,
            "正在加载..."
        )
    }
}